package org.redis.cluster.statistics;

import java.util.concurrent.atomic.AtomicInteger;

import org.redis.cluster.event.AbstractSessionEventListener;
import org.redis.cluster.transport.session.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ConnectionStatistics extends AbstractSessionEventListener {
	
	protected final Logger logger=LoggerFactory.getLogger(getClass());
	
	private final static AtomicInteger currentConnectionCounter=new AtomicInteger();
	
	private final static AtomicInteger connectCounter=new AtomicInteger();
	private final static AtomicInteger disconnectCounter=new AtomicInteger();

	protected void doSessionCreatedProcess(Session session) {
		if(logger.isInfoEnabled()) {
			logger.info("新连接 {},当前连接数  - {}",session.getRemoteAddress(),currentConnectionCounter.incrementAndGet());
		}
		
		connectCounter.incrementAndGet();
	}

	protected void doSessionClosedProcess(Session session) {
		if(logger.isInfoEnabled()) {
			logger.info("连接断开 {} ,当前连接数  - {}",session.getRemoteAddress(),currentConnectionCounter.decrementAndGet());
		}
		
		disconnectCounter.incrementAndGet();
	}

	/**
	 * 返回当前连接数
	 * @return
	 */
	public static int getCurrentConnectCount() {
		return currentConnectionCounter.get();
	}
	
	/**
	 * 返回连接数
	 * @return
	 */
	public static int getConnectCount() {
		return connectCounter.get();
	}
}